import { httpRequest } from 'fssc-template'
import { PREFIX } from '../constant/const'
import { xhrFile, downloadFileByBlob } from '@fssc/share'

// 下载本地导入模版
export function downloadlocalTemplate(fileName: string) {
  return new Promise((resolve, reject) => {
    xhrFile(`/templates/${fileName}`).then((blob) => {
      downloadFileByBlob(blob, fileName)
      resolve(true)
    }).catch(reject)
  })
}
export function uploadFile(data, uploadProgress) {
  return httpRequest.post(`${PREFIX.invoice}/file/upload`, data, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: uploadProgress })
}

// 获取表字段列表
export function getTableFieldList(tableName: string) {
  return httpRequest.get(`${PREFIX.invoice}/column/getTableColumnDetail/${tableName}`)
}

// 保存搜索过滤配置
export function saveCache(menuCode: string, config: any) {
  return httpRequest.post(`${PREFIX.invoice}/column/save`, { menuCode, columnJson: JSON.stringify(config) })
}

// 获取搜索过滤配置
export function getCache(code: string) {
  return httpRequest.get(`${PREFIX.invoice}/column/getConfig`, {code}).then((data: any) => {
    return Promise.resolve( data ? JSON.parse(data.columnJson) : data)
  })
}

export function getAreaList(data: any) {
  // 获取地址信息
  return httpRequest.get(`${PREFIX.invoice}/address-area/list`, data)
}
// 导入文件流返回url/fssc-invoice/file/upload
export {
  httpRequest,
  PREFIX
}


/**
 * @description: 下载分页
 * @param {any} data
 * @return {*}
 */
export function downloadPage(data: any) {
  return httpRequest.post(`${PREFIX.invoice}/downFileRecord/page`, data);
}

/**
 * @description: 下载接口
 * @param {any} data
 * @return {*}
 */
export function downloadRequest(data: any, fileName?:string) {
  return httpRequest.download(`${PREFIX.invoice}/downFileRecord/download`,
  { method: 'get', params: data, responseType: 'blob', downloadFileName:  fileName }

);
}
/**
 * @description: 首页统计
 * @param {any} data
 * @return {*}
 */
export function homeStatistics(data: any, fileName: string) {
  return httpRequest.get(`${PREFIX.invoice}/downFileRecord/homePage/countMessage`)
};

/**
 * @description: 根据url下载
 * @param {string} fileUrl
 * @param {string} fileName
 * @return {*}
 */
export function downloadByFileUrl(fileUrl: string, fileName?: string) {
  return httpRequest.download(`${PREFIX.invoice}/downFileRecord/downloadByUrl`, { method: 'get', params: { fileUrl }, responseType: 'blob', downloadFileName:  fileName });
}


/**
 * @description: 最近一次推送tag
 * @param {string} invoiceNo
 * @return {*}
 */
export function latestPush(invoiceNo: string) {
  return httpRequest.get(`${PREFIX.invoice}/invoice/latest-push-tag/${invoiceNo}`)
}

/**
 * @description: 推送发票
 * @param {any} data
 * @return {*}
 */
export function pushInvoice(data: any) {
  return httpRequest.post(`${PREFIX.invoice}/invoice/push-invoice-external`, data)
}

/**
 * @description: 查询优惠政策列表
 * @return {*}
 */
export function preferentialPolicy() {
  return httpRequest.get(`${PREFIX.invoice}/preferentialPolicy/selectList`, {}, {cacheFlag: true})
}